<template>
  <div class="info">
    <div class="head">
      <div>
        {{type==1? '新增':'编辑'}}缴费单位信息
      </div>
    </div>
    <div class="block">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="120px">
        <el-form-item label="单位名称" prop="name">
          <el-input style="width:250px" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="缴费编号" prop="code">
          <el-input style="width:250px" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="子账户号" prop="subAccount">
          <el-input style="width:250px" v-model="form.subAccount"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <el-button type="primary" v-if="type==1" @click="btnSave(1)">保存并新增下一个</el-button>
      <el-button type="primary" v-if="type==1" @click="btnSave(2)">保存</el-button>
      <el-button type="primary" v-if="type==2" @click="btnSave(3)">确定修改</el-button>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    let validaName = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入单位名称'))
      } else {
        callback()
      }
    }
    let validaCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入缴费编号'))
      } else {
        callback()
      }
    }
    let validaSubAccount = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入子账户号'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          { validator: validaName, trigger: 'blur' },
          { required: true, message: '请填写单位名称', trigger: 'blur' }
        ],
        code: [
          { validator: validaCode, trigger: 'blur' },
          { required: true, message: '请输入证件号码', trigger: 'blur' }
        ],
        subAccount: [
          { validator: validaSubAccount, trigger: 'blur' },
          { required: true, message: '请输入子账户号', trigger: 'blur' }
        ]
      } // 表单校验规则
    }
  },
  props: {
    callback: {
      type: Function,
      required: true
    },
    form: {
      type: Object,
      default: function () {
        return {}
      }
    },
    type: {
      type: Number,
      required: true
    }
  },
  methods: {
    btnSave (index) { // 保存
      this.$refs.form.validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form))
          if (index === 1) {
            this.$refs.form.resetFields()
          }
          this.callback(data, index)
        } else {
          console.log('err submit!!')
        }
      })
    },
    goPage () {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="less" scoped>
  .info {
    padding-left: 20px;
    font-size: 14px;
    .head {
      height: 60px;
      font-size: 16px;
      line-height: 60px;
      border-bottom: 1px solid #bfcbd9;
      margin-bottom: 20px;
      display: flex;
      justify-content: space-between;
    }
  }
</style>

